# Maintainer: Ben Orchard <thefirstmuffinman@gmail.com>

_realname=keystone
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=0.9.2
pkgrel=3
pkgdesc="Lightweight multi-platform, multi-architecture assembler framework (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
license=('spdx:GPL-2.0-only' 'custom')
url="https://www.keystone-engine.org/"
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/keystone-engine/keystone/archive/${pkgver}.tar.gz"
        "0001-cmake-install-dir.patch"
        "0002-kstool-win-msys2.patch"
        "0003-python-load-dll-from-prefix.patch")
sha256sums=('c9b3a343ed3e05ee168d29daf89820aff9effb2c74c6803c2d9e21d55b5b7c24'
            'f7aa8aff0a2fed76364b6a29e2fa09bff5bdde5cb839c45494f8bbd17bb38c3f'
            'a1754bd710dfbbedd49e186862bfd0e9f06fc2c60f8534f669ef6ca817641c27'
            '77077fb2a450605a2fefadb6fb9ba316167ba55fa13d044de80dbc3b9ddfb02f')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}"/0001-cmake-install-dir.patch
  patch -p1 -i "${srcdir}"/0002-kstool-win-msys2.patch
  patch -p1 -i "${srcdir}"/0003-python-load-dll-from-prefix.patch
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  cmake \
    -G Ninja \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=ON \
    -DLLVM_TARGETS_TO_BUILD=all \
    -DBUILD_LIBS_ONLY=OFF \
    "../${_realname}-${pkgver}"

  cmake --build .

  cd "${srcdir}/${_realname}-${pkgver}/bindings/python"
  "${MINGW_PREFIX}"/bin/python setup.py build
}

package_keystone() {
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")

  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" cmake --install .
  install -Dm0644 \
    "${srcdir}/${_realname}-${pkgver}/COPYING" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm0644 \
    "${srcdir}/${_realname}-${pkgver}/EXCEPTIONS-CLIENT" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/EXCEPTIONS-CLIENT"
}

package_python-keystone() {
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-keystone"
         "${MINGW_PACKAGE_PREFIX}-python")

  cd "${srcdir}/${_realname}-${pkgver}/bindings/python"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    "${MINGW_PREFIX}"/bin/python setup.py install --prefix="${MINGW_PREFIX}" \
      --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm755 sample.py -t "${pkgdir}${MINGW_PREFIX}/share/doc/python-${_realname}"
  install -Dm644 LICENSE.TXT -t "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
